package com.batchaps.classes;

import java.io.IOException;

import javax.microedition.amms.GlobalManager;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.batchaps.global.GameEngine;

public class My2DSpriteStruct extends MyEntity{

	private Image[] _texture2D;
    private int _ntexture2D;
    private int _itexture2D;

    private float _posY;
    private float _posX;
    private StructSprite structSprite;
    
    private int kindState;
    
    //blood
    private int blood;
    private Image iconAnimal;
    int iconX;
    int iconY;
    
    //bull
    My2DSprite bull;
    
    public My2DSpriteStruct(String textureName , int nTexture , float pX , float pY , StructSprite ss , String iconA , int iX , int iY , String ibull , KindOfMove kom)
    {
    	_texture2D = new Image[nTexture];
    	_ntexture2D = nTexture;
    	_itexture2D = 0;
    	_posX = pX;
    	_posY = pY;
    	structSprite = ss;
    	
    	
			try {
				for(int i = 0 ; i < _ntexture2D ; i++)
					_texture2D[i] = Image.createImage(textureName + i + ".png");
				
				iconAnimal = Image.createImage(iconA);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	
    	blood = 150;
    	iconX = iX;
    	iconY = iY;
    	
    	kindState = 1;
    	
    	//bull
    	bull = new My2DSprite( ibull , (int)_posX, (int)_posY, kom);

    }
    
    public void PlayBullet(double v)
    {
    	bull.isPlay = true;
    	bull.v0 = v;
    }
    
    public boolean IsClick(int x, int y)
    {

            if ((x >= _posX && x <= _posX + _texture2D[_itexture2D].getWidth()) &&
                (y >= _posY && y <= _posY + _texture2D[_itexture2D].getHeight()))
            {
                return true;
            }
            else
            {
                return false;
            }
    }
    
    public void Update()
    {
    	bull.Update();
    	switch(kindState)
    	{
	    	case 1://stand
	    		if (_itexture2D < structSprite.beginStand || _itexture2D >= structSprite.endStand)
	    			_itexture2D = structSprite.beginStand;
	            else
	            	_itexture2D++;
	    		break;
	    	case 2://wait
	    		if (_itexture2D < structSprite.beginWait || _itexture2D >= structSprite.endWait)
	    			_itexture2D = structSprite.beginWait;
	            else
	            	_itexture2D++;
	    		break;
	    	case 3://Laugh
	    		if (_itexture2D < structSprite.beginLaugh || _itexture2D >= structSprite.endLaugh)
	    			_itexture2D = structSprite.beginLaugh;
	            else
	            	_itexture2D++;
	    		break;
	    	case 4://StrongHit
	    		if (_itexture2D < structSprite.beginStrongHit || _itexture2D >= structSprite.endStrongHit)
	    			_itexture2D = structSprite.beginStrongHit;
	            else
	            	_itexture2D++;
	    		break;
	    	case 5://WeakHit
	    		if (_itexture2D < structSprite.beginWeakHit || _itexture2D >= structSprite.endWeakHit)
	    			_itexture2D = structSprite.beginWeakHit;
	            else
	            	_itexture2D++;
	    		break;
	    	case 6://ClampedCrab
	    		if (_itexture2D < structSprite.beginClampedCrab || _itexture2D >= structSprite.endClampedCrab)
	    			_itexture2D = structSprite.beginClampedCrab;
	            else
	            	_itexture2D++;
	    		break;
	    	case 7://PrepareThrow
	    		if (_itexture2D < structSprite.beginPrepareThrow || _itexture2D >= structSprite.endPrepareThorw)
	    			_itexture2D = structSprite.beginPrepareThrow;
	            else
	            	_itexture2D++;
	    		break;
	    	case 8://win
	    		_itexture2D = structSprite.win;
	    		break;
	    	case 9://lose
	    		_itexture2D = structSprite.lose;
	    		break;
    	}
    }
    
    public void Draw(Graphics g)
    {
    	g.drawImage(_texture2D[_itexture2D], (int)_posX, (int)_posY, 0);
    	//draw icon
    	g.drawImage(iconAnimal, iconX, iconY, 0);
    	
    	g.setColor(255, 0, 0);
    	g.drawRect(iconX, iconY + + GameEngine.IconSize + 5, 15, 150);
    	g.setColor(255, 255, 0);
    	g.fillRect(iconX+1, iconY + + GameEngine.IconSize + 5, 14, blood);
    	
    	bull.Draw(g, 0);

    }
    
	public Image[] get_texture2D() {
		return _texture2D;
	}
	public void set_texture2D(Image[] _texture2D) {
		this._texture2D = _texture2D;
	}
	public int get_ntexture2D() {
		return _ntexture2D;
	}
	public void set_ntexture2D(int _ntexture2D) {
		this._ntexture2D = _ntexture2D;
	}
	public int get_itexture2D() {
		return _itexture2D;
	}
	public void set_itexture2D(int _itexture2D) {
		this._itexture2D = _itexture2D;
	}
	public float get_posY() {
		return _posY;
	}
	public void set_posY(float _posY) {
		this._posY = _posY;
	}
	public float get_posX() {
		return _posX;
	}
	public void set_posX(float _posX) {
		this._posX = _posX;
	}
	public StructSprite getStructSprite() {
		return structSprite;
	}
	public void setStructSprite(StructSprite structSprite) {
		this.structSprite = structSprite;
	}

	public int getKindState() {
		return kindState;
	}

	public void setKindState(int kindState) {
		this.kindState = kindState;
	}

	public int getBlood() {
		return blood;
	}

	public void setBlood(int blood) {
		this.blood = blood;
	}
    
    
}
